package hjw.com.controller.adm;

import hjw.com.dao.ClzMapper;
import hjw.com.dao.CourseMapper;
import hjw.com.dao.TaskMapper;
import hjw.com.dao.UserMapper;
import hjw.com.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/adm/task")
@SuppressWarnings("all")
public class AdmTaskController {

    @Autowired
    ClzMapper clzMapper;
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    CourseMapper courseMapper;

    @RequestMapping("/show")
    public String show(Model model){

        List<User> teas = userMapper.findByRole("tea");
        List<Clz> clzs = clzMapper.findAll();
        List<Course> courses = courseMapper.findAll();
        List<TaskInfo> taskInfos = taskMapper.findAll();

        model.addAttribute("teas",teas);
        model.addAttribute("clzs",clzs);
        model.addAttribute("courses",courses);
        model.addAttribute("taskInfos",taskInfos);

        return "adm/task";
    }

    @GetMapping("/go2Add")
    public String go2Add(){
        return "forward:/adm/task/show";
    }

    @RequestMapping("/doAdd")
    public String doAdd(Task task1, Model m){
        System.out.println("task1 =" + task1);
        Task task = taskMapper.find(task1);
        System.out.println("task = " + task);
        if (task != null){
            m.addAttribute("state","(教师："+task1.getTid()+"已有排课记录)");
            return "forward:/adm/task/show";
        }
        int key = 0;
        try {
            key = taskMapper.add(task1);
        } catch (Exception e) {

        }
        if (key > 0){
            m.addAttribute("state","(已为"+task1.getTid()+"号教师排课)");
        }
        if (key <= 0){
            m.addAttribute("state","(数据异常未能排课!!!)");
        }

        return "forward:/adm/task/show";
    }

    @GetMapping("/del/{kid}")
    public String del(@PathVariable("kid") Integer id, Model m){
        int key = taskMapper.remove(id);
        if (key > 0){
            m.addAttribute("state","(已取消教师排课)");
        }else {
            m.addAttribute("state","(无法取消教师排课)");
        }
        return "forward:/adm/task/show";
    }
}